// pages/planter/myTree.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
      type: 0,
      page: 0,
      show:!0,
      treeType: [
        {
          type:1,
          title:"樟科"
        },
        {
          type:2,
          title:"桃科"
        },
        {
          
        }
      ],
      treeList:[],
      list:[],
      userid: wx.BaaS.storage.get('userId')
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
    this.getPlanterTree();
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  switchChange: function(e){
    let t = this;
    let treeid = e.target.dataset.treeid;
    var url = app.getUrl()+'/planttingTree/tree';
    var data = {
      type:'updataPlanterTree' ,
      wxuserid:wx.BaaS.storage.get('userId'),
      treeid: treeid
    };
    app.core(url,'GET',data,function(res){
      console.log(res.data);
    })
    // let tableID = 48778;
    // let Product = new wx.BaaS.TableObject(tableID);

    
    // let query1 = new wx.BaaS.Query();
    // let query2 = new wx.BaaS.Query()
    // query1.compare('userid', '=', this.data.userid);
    // query2.compare('treeid', '=', treeid);
    // let andQuery = wx.BaaS.Query.and(query1, query2);

    
    // Product.setQuery(andQuery).find().then(res => {
    //   var treeLists = res.data.objects;
    //   if (treeLists.length == 0){
    //     let MyTableObject = new wx.BaaS.TableObject(tableID)
    //     let MyRecord = MyTableObject.create();
    //     MyRecord.set({
    //       userid: t.data.userid,
    //       treeid: treeid
    //     })
    //     MyRecord.save();
    //   }else{
    //     let product = Product.getWithoutData(treeLists[0].id);
    //     var open = parseInt(treeLists[0].status) === 1 ? '0':'1';
    //     product.set('status', open);
    //     product.update().then(res => {
    //       console.log('选择成功')
    //     }, err => {
    //       // err
    //     })
    //   }
    // }, err => {
      
    // })
  },
  lower  : function(e){//上拉加载  每6个加载一次
    var list = this.data.list;
    var listArray = this.moreLoad(this.data.treeList, 6);
    var page = this.data.page + 1;
    if (listArray.length >= page + 1) {//数组长度>=等于页数的时候 加载完成全部
      this.setData({
        list: list.concat(listArray[page]),
        page: page
      });
    } else {
      this.setData({
        idLoadList: !0
      })
    }
  },  
  changeType : function(e){
    let treetype = e.target.dataset.type;
    this.setData({
      type: parseInt(treetype)
    });
    this.getTreeList(treetype);
  },
  getTreeList : function(treetype){
    var t =this;
    var url = app.getUrl() + '/planttingTree/tree';
    var data = {
      type:'getTree',
      typeid: treetype
    };
    app.core(url,'GET',data,function(res){
      var treeLists = res.data;//获取树的数据
      treeLists = t.getStatus(treeLists);
      var listArray = t.moreLoad(treeLists, 6);
      if (treeLists.length>0){
        t.setData({
          treeList :treeLists,
          list: listArray[0]
        });
      }
    })
  },
  moreLoad: function (array, size) {//获取第N页的数据
    var result = [];//定义一个数组
    for (var x = 0; x < Math.ceil(array.length / size); x++) {//Math.ceil取整
      var start = x * size;//开始位置
      var end = start + size;//结束位置
      result.push(array.slice(start, end));//slice 获取数组的某一段区域  push 往数组里添加额外的值
    }
    return result;
  },
  getPlanterTree: function(obj){//获取植树人的全部树
    var t = this;
    var url = app.getUrl() + '/planttingTree/tree';
    var data = {
      type: 'getPlanterTree',
      wxuserid: wx.BaaS.storage.get('userId')
    };
    app.core(url, 'GET', data, function (res) {
      t.setData({
        planterTree : res.data,
        show: !1
      })
      t.getTreeList(0);
    })
  },
  getStatus: function (treeList){
    var platerTree = this.data.planterTree;
    for (var i = 0; i < treeList.length; i++){
      for (var j = 0; j < platerTree.length;j++){
        if (treeList[i].id === platerTree[j].treeid){
          treeList[i].open = parseInt(platerTree[j].status) === 1 ? 'checked':'';
          break;
        }
      }
    }
    return treeList;
  }
})